package com.ygsj;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.Guideline;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ygsj.ui.loader.Loader;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    private String userName_db,pwd;//userName_db为系统管理员的账号;
    private String dbname;//数据库名称;
    private String imei, name_person;//edittv填入的用户名;
    private String mmname;//站点名称
    private String uuname;//数据库返回的用户名;
    private android.widget.TextView tv_station,tv_username;
    private android.widget.Button btn_yardConfirm,btn_emptyLeave,btn_regedit;
    public long startTime;
    private String confirmPower, goOutPower;//权限

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
        initData();
    }

    private void intiView() {
        this.btn_regedit = (Button) findViewById(R.id.btn_regedit);
        this.btn_emptyLeave = (Button) findViewById(R.id.btn_emptyLeave);
        this.btn_yardConfirm = (Button) findViewById(R.id.btn_yardConfirm);
        this.tv_username = (TextView) findViewById(R.id.tv_username);
        this.tv_station = (TextView) findViewById(R.id.tv_station);
        btn_regedit.setOnClickListener(this);
        btn_emptyLeave.setOnClickListener(this);
        btn_yardConfirm.setOnClickListener(this);

    }

    private void initData() {
        startTime = System.currentTimeMillis();
        userName_db = getIntent().getStringExtra("userName");
        pwd = getIntent().getStringExtra("pwd");
        dbname = getIntent().getStringExtra("dbname");
        name_person = getIntent().getStringExtra("name");//edittv填入的用户名;
        mmname = getIntent().getStringExtra("mmname");////站点名称
        uuname = getIntent().getStringExtra("uuname");//数据库返回的用户名;
        imei = getIntent().getStringExtra("imei");//数据库返回的用户名;
        confirmPower = getIntent().getStringExtra("confirmPower");
        goOutPower = getIntent().getStringExtra("goOutPower");

        if (!TextUtils.isEmpty(mmname)) tv_station.setText(mmname); else tv_station.setText(getResources().getText(R.string.EmptySite));
        if (!TextUtils.isEmpty(uuname)) tv_username.setText(uuname);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_regedit:
                Intent intent = new Intent(MainActivity.this,SignUpActivity.class);
                addExtra(intent);
                startActivity(intent);
                break;

            case R.id.btn_emptyLeave:
                if (Integer.parseInt(goOutPower) > 0){
                Loader.showLoading(this);
                Intent intent2 = new Intent(MainActivity.this,ConfirmActivity.class);
                addExtra(intent2);
                intent2.putExtra("flag",GloableUrl.EMPTY_OUT);
                startActivity(intent2);
                }else {
                    Toast.makeText(this, "没有此权限", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.btn_yardConfirm:
                if (Integer.parseInt(confirmPower) > 0){
                    Loader.showLoading(this);
                    Intent intent1 = new Intent(MainActivity.this,ConfirmActivity.class);
                    addExtra(intent1);
                    intent1.putExtra("flag",GloableUrl.YARD_CONFIRM);
                    startActivity(intent1);
                }else {
                    Toast.makeText(this, "没有此权限", Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

    public void  addExtra(Intent intent){
        intent.putExtra("userName", userName_db);
        intent.putExtra("pwd",pwd);
        intent.putExtra("dbname",dbname);
        intent.putExtra("name",name_person);
        intent.putExtra("mmname",mmname);
        intent.putExtra("uuname",uuname);
        intent.putExtra("imei",imei);
    }

    /*两次回退键退出app*/
    private boolean canExit = false;
    @Override
    public void finish() {
        if (canExit) {
            System.exit(0);
            super.finish();
        } else {
            Toast.makeText(getApplicationContext(), "再次按键退出程序", Toast.LENGTH_SHORT).show();
            canExit = true;
            handlerRunTime.postDelayed(new Runnable() {
                @Override
                public void run() {
                    canExit = false;
                }
            }, 2000);
        }
    }

    Handler handlerRunTime = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handlerRunTime.postDelayed(this, 0);//this指的是runnable,延迟0秒执行,可以实现定时器的效果;
            long nowTime = System.currentTimeMillis();
            long runTime = nowTime - startTime;//startTime点击开始执行
            long totalSeconds = runTime / 1000;
            //求出现在的毫秒
            long currentSss = (runTime % 1000);//点击数耗费的毫秒数;
        }
    };
}
